package com.backwaveweibo.bww.controller;

import com.backwaveweibo.bww.utils.Result;
import com.backwaveweibo.bww.utils.ResultCode;
import com.backwaveweibo.bww.utils.ResultGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.qiniu.common.QiniuException;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.io.IOException;

@RestControllerAdvice
public class ExceptionController {
    /**
     * 授权异常返回结果
     *
     * @return
     */
    @ExceptionHandler(value = AuthorizationException.class)
    public ResponseEntity authorizationException() {
        return new ResponseEntity(new Result().setMessage("您没有这个权限哦亲!"), HttpStatus.UNAUTHORIZED);
    }

    /**
     * 密码错误异常返回
     *
     * @return
     */
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public Result incorrectCredentialsException() {
        return new Result().setCode(ResultCode.FAIL).setMessage("用户名或密码错误");
    }

    @ExceptionHandler(value = AuthenticationException.class)
    public Result authenticationException() {
        return new Result().setCode(ResultCode.FAIL).setMessage("用户名或密码错误");
    }

    @ExceptionHandler(value = ExpiredJwtException.class)
    public Result expiredJwtException() {
        return new Result().setCode(ResultCode.FAIL).setMessage("token过期请重新登陆");
    }

    @ExceptionHandler(value = IOException.class)
    public Result ioException(IOException e) {
        return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR, "系统错误==>" + e.getMessage(), false);
    }

    @ExceptionHandler(value = NullPointerException.class)
    public Result nullPointerException(NullPointerException e) {
        return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR, "系统错误==>" + e.getMessage(), false);
    }

    @ExceptionHandler(value = QiniuException.class)
    public Result qiniuException() {
        return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR, "文件传输失败，请重试", false);
    }

    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    public Result fileMaxSizeException() {
        return ResultGenerator.genFailResult(ResultCode.FAIL, "单个文件必须小于2MB,总共不能大于20MB", false);
    }

    @ExceptionHandler(value = JsonProcessingException.class)
    public Result jsonProcessingException() {
        return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR, "Json处理异常", false);
    }

    @ExceptionHandler(value = Exception.class)
    public Result exception(Exception e) {
        return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR, "系统错误==>" + e.getMessage(), false);
    }


}
